package com.totcy.atom.model;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.AssetManager;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.totcy.atom.bean.ImagesBean;
import com.totcy.atom.bean.PaperBean;
import com.totcy.atom.bean.response.HttpResponse;
import com.totcy.atom.net.HttpManager;
import com.totcy.atom.net.exception.ApiException;
import com.totcy.atom.presenter.contract.PaperContract;
import com.totcy.atom.utils.AssetUtil;
import com.totcy.baselibrary.base.mvpbase.BaseModelImpl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * description :  Paper Model
 *
 * @author : Totcy
 * @date : 2018/11/8 13:35
 */
public class PaperModel extends BaseModelImpl implements PaperContract.Model {

    private Gson gson = new Gson();

    public PaperModel(Context context) {
        super(context);
    }

    @Override
    public void canCelRequest() {

    }

    @SuppressLint("CheckResult")
    @Override
    public void getPaperList(final String subjectName, final int type, final DataCallback callback) {

        Observable.create(new ObservableOnSubscribe<HttpResponse<List<PaperBean>>>() {
            @Override
            public void subscribe(ObservableEmitter<HttpResponse<List<PaperBean>>> emitter) throws Exception {

                String json = AssetUtil.getJson(mContext, "subject/papers/" + subjectName + "_" + type + ".json");
                HttpResponse<List<PaperBean>> response = gson.fromJson(
                        json,
                        new TypeToken<HttpResponse<List<PaperBean>>>() {
                        }.getType());

                emitter.onNext(response);
            }
        })
                .map(new Function<HttpResponse<List<PaperBean>>, List<PaperBean>>() {
                    @Override
                    public List<PaperBean> apply(HttpResponse<List<PaperBean>> response) throws Exception {
                        if (response.isSuccessful()) {
                            return response.getData();
                        } else {
                            throw new ApiException(response.getCode(), response.getMessage());
                        }
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<PaperBean>>() {
                    @Override
                    public void accept(List<PaperBean> paperBeans) throws Exception {
                            callback.onPaperList(paperBeans);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                        callback.onDataNotAvailable(-1, throwable.getMessage());
                    }
                });
    }

}
